package com.example.javatwo.service.xs.dh;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.example.javatwo.mapper.cg.lmx.WuLiaoMapper;
import com.example.javatwo.mapper.xs.dh.DsdssaleMainDao;
import com.example.javatwo.mapper.xs.dh.KehuDao;
import com.example.javatwo.pojo.cg.lmx.Product;
import com.example.javatwo.pojo.xs.dh.DsdssaleMain;
import com.example.javatwo.pojo.xs.dh.DsdssaleSub;
import com.example.javatwo.util.PageBead;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class DsdssaleMainBiz implements IDsdssaleMainBiz {

	@Autowired
	private KehuDao kehuDao;

	@Autowired
	private DsdssaleMainDao dao;

	@Autowired
	private WuLiaoMapper wuliaoDao;
	
	public WuLiaoMapper getWuliaoDao() {
		return wuliaoDao;
	}

	public void setWuliaoDao(WuLiaoMapper wuliaoDao) {
		this.wuliaoDao = wuliaoDao;
	}

	public DsdssaleMainDao getDao() {
		return dao;
	}

	public void setDao(DsdssaleMainDao dao) {
		this.dao = dao;
	}

	public KehuDao getKehuDao() {
		return kehuDao;
	}

	public void setKehuDao(KehuDao kehuDao) {
		this.kehuDao = kehuDao;
	}

	@Override
	public int saveDsdssaleMain(DsdssaleMain po) {
		DsdssaleMain x = dao.findDsdssaleMain(po.getBillno());
		if (x != null) {
			if ("已审核".equals(x.getUdef2())) {
				return 0;
			}
			this.delDsdssaleMain(po.getBillno());
		}
				
		for (DsdssaleSub temp : po.getList()) {
			temp.setBillno(po.getBillno());
		}

		dao.saveDsdssaleMain(po);
		return 1;
	}

	@Override
	public DsdssaleMain findDsdssaleMain(String id) {
		return dao.findDsdssaleMain(id);
	}

	@Override
	public int delDsdssaleMain(String id) {
		DsdssaleMain x = dao.findDsdssaleMain(id);
		if ("已审核".equals(x.getUdef2())) {
			return 0;
		}
		dao.delDsdssaleMain(id);
		return 1;
	}

	@Override
	public DsdssaleMain findMaxDateResult() {
		return dao.findMaxDateResult();
	}

	@Override
	public PageBead findPageBead(Integer pageNum) {
		PageBead p = new PageBead();
		p.setEveryPage(1);
		p.setCurrentPage(pageNum);
		p.setTotalCount(dao.findCount());
		List<DsdssaleMain> list = dao.findPage(pageNum - 1);
		p.setData(list);
		return p;
	}

	@Override
	public int shenheClick(DsdssaleMain p) {
		DsdssaleMain x = dao.findDsdssaleMain(p.getBillno());
		if ("已审核".equals(x.getUdef2())) {
			return 0;
		}
		// 1、修改商品库存-总成本
		for (DsdssaleSub s : p.getList()) {
			dao.updateWuliao(s.getPridid(), s.getSquantity().intValue());
		}
		// 2、修改客户账款剩余额度-期末应收款
		kehuDao.updateMoney(p.getCustid(), p.getUdef1());
		// 3、修改退货表审核状态
		dao.updateShekeTuihuo(p.getBillno(),"已审核");
		return 1;
	}

	@Override
	public int shenheCase(DsdssaleMain p) {
		DsdssaleMain x = dao.findDsdssaleMain(p.getBillno());
		if ("未审核".equals(x.getUdef2())) {
			return 0;
		}
		//1、查询该商品库存，如果小于1则continue;
		for (DsdssaleSub s : p.getList()) {
			Product a=wuliaoDao.selectId(s.getPridid());
			System.out.println(a);
			if(a.getCallAmt()<1){
				return 0;
			}
		}
		
		for (DsdssaleSub s : p.getList()) {
			//2、修改商品库存
			dao.updateWuliao(s.getPridid(), (s.getSquantity().intValue())*-1);
		}
		//3、修改客户账款剩余额度->期末应收款
		kehuDao.updateMoney(p.getCustid(), (p.getUdef1()*-1));
		//4、审核状态
		dao.updateShekeTuihuo(p.getBillno(),"未审核");
		return 1;
	}

}
